//
//  ByCellOrder.m
//  oc_knowledge
//
//  Created by mc on 17/1/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellOrder.h"
#define TagColor [UIColor colorWithR:231 g:142 b:142]
@interface ByCellOrder()
{
    UIImageView *_proImgView;           // 商品图片
    UILabel     *_titleLabel;           // 商品描述
    UILabel     *_priceLabel;           // 价格
    UILabel     *_originPriceLabel;     // 原价
    UILabel     *_accountLabel;         // 数量
    UILabel     *_attributeLabel;       // 商品属性
    UILabel     *_tagLabel;             // 商品的标签属性
    UILabel     *_orderStatusLabel;     // 订单状态
    UIView      *_lineView;             // 白色线条
    UIButton    *_bottomButton;         // 根据订单属性判断是否有按钮点击
}
@end
@implementation ByCellOrder
+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByOrderCell";
    ByCellOrder *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        [self setViews];
    }
    return self;
}
#pragma mark ---创建视图
- (void)setViews{
    _proImgView = [[UIImageView alloc] init];
    [self addSubview:_proImgView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.font = FONT(14);
    _titleLabel.numberOfLines = 2;
    [self addSubview:_titleLabel];
    
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.textColor = [UIColor blackColor];
    _priceLabel.font = FONT(14);
    _priceLabel.adjustsFontSizeToFitWidth = YES;
    _priceLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_priceLabel];
    
    _originPriceLabel = [[UILabel alloc] init];
    _originPriceLabel.textColor = [UIColor lightGrayColor];
    _originPriceLabel.font = FONT(14);
    _originPriceLabel.adjustsFontSizeToFitWidth = YES;
    _originPriceLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_originPriceLabel];
    
    _attributeLabel = [[UILabel alloc] init];
    _attributeLabel.textColor = [UIColor lightGrayColor];
    _attributeLabel.font = FONT(13);
    [self addSubview:_attributeLabel];
    
    _accountLabel = [[UILabel alloc] init];
    _accountLabel.textColor = [UIColor lightGrayColor];
    _accountLabel.font = FONT(12);
    _accountLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_accountLabel];
    
    
    _tagLabel = [[UILabel alloc] init];
    _tagLabel.font = FONT(13);
    _tagLabel.textColor = TagColor;
    _tagLabel.layer.borderColor = TagColor.CGColor;
    _tagLabel.layer.borderWidth = 1;
    _tagLabel.layer.cornerRadius = 4.0;
    [self addSubview:_tagLabel];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = BY_THEME_COLOR;
    [self addSubview:_lineView];
    
    _bottomButton = [[UIButton alloc] init];
    _bottomButton.backgroundColor = [UIColor whiteColor];
    _bottomButton.layer.borderColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
    _bottomButton.titleLabel.font = FONT(14);
    _bottomButton.layer.cornerRadius = 4.0;
    _bottomButton.layer.borderWidth = 1;
    [self addSubview:_bottomButton];
    

}

#pragma mark --
-(void)setModel:(ByModelOrderListItemProduct *)model{
    _model = model;
    
    CGFloat x,y,width,height;
    x = 0;
    y = 0;
    width = SCREEN_WIDTH;
    height = 2;
    _lineView.frame = CGRectMake(x, y, width, height);
    _lineView.backgroundColor = [UIColor whiteColor];
    //图片
    x = 10;
    y = 7;
    width = height = 80;
    _proImgView.frame = CGRectMake(x, y, width, height);
    [_proImgView setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:DEFAULT_IMAGE];
  
    //价格
    NSString *str = [NSString stringWithFormat:@"%.2lf",[_model.price floatValue]];
    width = [str getSizeOfString:14].width + 10;
    height = 15;
    x = SCREEN_WIDTH - width - x;
    _priceLabel.frame = CGRectMake(x, y, width, height);
    _priceLabel.text = str;
    
    //原价格
    str = [NSString stringWithFormat:@"%.2lf",[_model.ori_price floatValue]];
    width = [str getSizeOfString:14].width + 10;
    x = SCREEN_WIDTH - width - 10;
    y = _priceLabel.bottom + 5;
    _originPriceLabel.frame = CGRectMake(x, y, width, height);
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%.2lf", [_model.ori_price floatValue]]];
    [attr addAttribute:NSStrikethroughStyleAttributeName
                 value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)
                 range:NSMakeRange(0, attr.length)];
    _originPriceLabel.attributedText = attr;

    
    //title
    x = _proImgView.right + 10;
    y = 10;
    width = _priceLabel.width > _originPriceLabel.width ? _priceLabel.width:_originPriceLabel.width;
    width = SCREEN_WIDTH - _proImgView.right - width - 30;
    height = [_model.name getRectOfStringWithSize:CGSizeMake(width, MAXFLOAT) font:14].size.height;
    _titleLabel.frame = CGRectMake(x, y, width, height);
    _titleLabel.text = model.name;

    //属性
    y = _titleLabel.bottom + 5;
    width = _titleLabel.width;
    height = 15;
    _attributeLabel.frame = CGRectMake(x, y, width, height);
    _attributeLabel.text = _model.sku_desc;
    
    //标签属性
    y = _attributeLabel.bottom + 2;
    if (_model.tag.length != 0) {
        width = [_model.tag getSizeOfString:13].width;
    }else{
        width = 0;
        height = 0;
    }
    _tagLabel.frame = CGRectMake(x, y, width, height);
    _tagLabel.text  = _model.tag;
    
    //数量
    str = [NSString stringWithFormat:@"×%@",_model.count];
    width = [str getSizeOfString:14].width + 10;
    height = 15;
    x = SCREEN_WIDTH - 10 - width;
    y = _originPriceLabel.bottom + 5;
    _accountLabel.frame = CGRectMake(x, y, width, height);
    _accountLabel.text = str;
    
    /*** 判断是否有按钮点击 */
    NSLog(@"%@",_model.order_status);
    NSInteger order_status = _model.order_status.integerValue;
    y = _proImgView.bottom + 10;
    //先判断是否是详情界面
    if (_model.isFromDetail && order_status != 1 && order_status != 8) {
        width = [@"删除订单" getSizeOfString:14].width + 20;
        x = SCREEN_WIDTH - 10 - width;
        height = 30;
        if (order_status == 2 || order_status == 3 ) {
           str = @"退款";
        }else if(order_status == 4){
            str = @"申请售后";
        }
    }else{
        x = 0;
        width = height = 0;
    }
    _bottomButton.frame = CGRectMake(x, y, width, height);
    [_bottomButton setTitle:str forState:UIControlStateNormal];
    [_bottomButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_bottomButton addTarget:self action:@selector(refundAction:) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark --- 退款或者售后
- (void)refundAction:(ByModelOrderListItemProduct *)model{
    if (self.refundBlock) {
        self.refundBlock(model);
    }
}
@end
